package com.caifeng.weatheractivity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 解析raw下XML文件 示例
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    TextView weather, wind, temperature, pm, name;
    Button cq, sh, bj;
    ImageView icon;
    List<Map<String, String>> list;
    Map<String, String> map;
    String strTemp,strPm;
    String strWeather;//天气
    String strCity;//城市
    String strWind;//风力

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.weather);

        init();
        try {
            InputStream is = this.getResources().openRawResource(R.raw.weather_1);
            List<WeatherInfo> weatherInfos = WeatherService.getInfoFromXml(is);
            list = new ArrayList<>();
            for (WeatherInfo info : weatherInfos) {
                map = new HashMap<>();
                map.put("temp", info.getTemp());
                map.put("weather", info.getWeather());
                map.put("name", info.getCity());
                map.put("pm", info.getPm());
                map.put("wind", info.getWind());
                list.add(map);
            }
        } catch (Exception e) {

            e.printStackTrace();
            Toast.makeText(this, "数据没有解析成功", Toast.LENGTH_SHORT).show();
        }
        getMap(1, R.mipmap.qingtian);
    }

    private void init() {
        cq = findViewById(R.id.cq);
        icon = findViewById(R.id.weather_icon);
        bj = findViewById(R.id.bj);
        sh = findViewById(R.id.sh);
        name = findViewById(R.id.city);
        weather = findViewById(R.id.weather_text);
        wind = findViewById(R.id.wind);
        pm = findViewById(R.id.pm);
        temperature = findViewById(R.id.temperature);

        cq.setOnClickListener(this);
        bj.setOnClickListener(this);
        sh.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.cq:
                getMap(0, R.mipmap.duoyun);
                break;
            case R.id.sh:
                getMap(1, R.mipmap.qingtian);
                break;
            case R.id.bj:
                getMap(2, R.mipmap.yintian);
                break;
        }
    }
    private void getMap(int number,int iconNumber){
        Map<String,String> cityMap=list.get(number);
        strTemp=cityMap.get("temp");
        strCity=cityMap.get("name");
        strPm=cityMap.get("pm");
        strWind=cityMap.get("wind");
        strWeather=cityMap.get("weather");

        name.setText(strCity);
        Log.e("名字",strCity);
        icon.setImageResource(iconNumber);
        weather.setText(strWeather);//天气
        pm.setText("PM："+strPm);
        wind.setText("风力:"+strWind);
        temperature.setText("温度："+strTemp);

    }






}













